package com.mygdx.game_world;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.mygdx.game_helper.AssetLoader;
import com.mygdx.game_objects.Basket;
import com.mygdx.game_objects.Bird;

public class GameRenderer {

	private GameWorld myWorld;
	private OrthographicCamera cam;
	private ShapeRenderer shapeRenderer;
	private SpriteBatch batcher;

	private ArrayList<Bird> birds;
	private Basket basket;
	
	private int gameWidth;
	private int gameHeight;

	public GameRenderer(GameWorld myWorld, int gameWidth, int gameHeight) {
		this.myWorld = myWorld;
		this.gameWidth = gameWidth;
		this.gameHeight = gameHeight;
		cam = new OrthographicCamera();
		cam.setToOrtho(true, gameWidth, gameHeight);

		shapeRenderer = new ShapeRenderer();
		shapeRenderer.setProjectionMatrix(cam.combined);
		batcher = new SpriteBatch();
		batcher.setProjectionMatrix(cam.combined);

		initObjects();
	}

	private void initObjects() {
		birds = myWorld.getBirds();
		basket = myWorld.getBasket();
	}

	private void drawBirdsShape() {
		if (birds.size() > 0) {
			for (Bird b : birds) {
				if (b.getType().equalsIgnoreCase("chicken"))
					shapeRenderer.setColor(Color.YELLOW);
				else if (b.getType().equalsIgnoreCase("duck"))
					shapeRenderer.setColor(Color.WHITE);
				else if (b.getType().equalsIgnoreCase("swan"))
					shapeRenderer.setColor(Color.BLUE);
				shapeRenderer.rect(b.getX(), b.getY(), b.getWidth(), b.getHeight());
			}
		}
	}

	private void drawEggsShape() {
		if (birds.size() > 0) {
			for (Bird b : birds) {
				if (b.getEgg() != null) {
					if (b.getEgg().isCatched())
						continue;
					if (b.getEgg().isShit())
						shapeRenderer.setColor(Color.DARK_GRAY);
					else
						shapeRenderer.setColor(Color.ORANGE);
					shapeRenderer.circle(b.getEgg().getX(), b.getEgg().getY(), b.getEgg().getRadius());
				}
			}
		}
	}

	private void drawBasketShape() {
		shapeRenderer.setColor(Color.GREEN);
		shapeRenderer.rect(basket.getX(), basket.getY()+10, basket.getWidth(), basket.getHeight()-10);
	}

	public void render(float delta) {
		// draw black background
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

//		shapeRenderer.begin(ShapeType.Filled);
//
//		// drawBirds
////		drawBirdsShape();
////		drawEggsShape();
////		drawBasketShape();
//
//		shapeRenderer.end();

		batcher.begin();
		batcher.disableBlending();
		batcher.draw(AssetLoader.bg, 0, 0, gameWidth, gameHeight);
		batcher.enableBlending();
		drawBirds();
		drawEggs();
		drawBasket();
		
		batcher.end();
		

	}

	private void drawBasket() {
		// TODO Auto-generated method stub
		batcher.draw(AssetLoader.basket, basket.getX(), basket.getY(), basket.getWidth(), basket.getHeight());
	}

	private void drawEggs() {
		// TODO Auto-generated method stub
		if (birds.size() > 0) {
			for (Bird b : birds) {
				if (b.getEgg() != null) {
					if (b.getEgg().isCatched())
						continue;
					if (b.getEgg().isShit())
						batcher.draw(AssetLoader.shit, b.getEgg().getX() - b.getEgg().getWidth() / 2, b.getEgg().getY()
								- b.getEgg().getHeight() / 2, b.getEgg().getWidth(), b.getEgg().getHeight());
					else
						batcher.draw(AssetLoader.eggs, b.getEgg().getX() - b.getEgg().getWidth() / 2, b.getEgg().getY()
								- b.getEgg().getHeight() / 2, b.getEgg().getWidth(), b.getEgg().getHeight());
				}
			}
		}
	}

	private void drawBirds() {
		// TODO Auto-generated method stub
		if (birds.size() > 0) {
			for (Bird b : birds) {
				if (b.getType().equalsIgnoreCase("chicken"))
					batcher.draw(AssetLoader.chicken, b.getX(), b.getY(), b.getWidth(), b.getHeight());
				else if (b.getType().equalsIgnoreCase("duck"))
					batcher.draw(AssetLoader.duck, b.getX(), b.getY(), b.getWidth(), b.getHeight());
				else if (b.getType().equalsIgnoreCase("swan"))
					batcher.draw(AssetLoader.swan, b.getX(), b.getY(), b.getWidth(), b.getHeight());
			}
		}
	}
}
